#ifndef CODEEDITOROVERLAY_H
#define CODEEDITOROVERLAY_H

#include <QtGui/QWidget>
#include "CodeEditor.h"

namespace CodeEdit {
namespace Internal {

struct OverlaySelection
{
    OverlaySelection()
        : fixedLength(-1)
        , dropShadow(false)
        , expandBegin(false)
        , verticalBlockSelection(0) { }

    QTextCursor cursorBegin;
    QTextCursor cursorEnd;
    QColor fg;
    QColor bg;
    int fixedLength;
    bool dropShadow;
    bool expandBegin;
    int verticalBlockSelection;
};

class CodeEditorOverlay : public QObject
{
    Q_OBJECT

    CodeEditor *_editor;
    QWidget *_viewport;

public:
    QList<OverlaySelection> _selections;

private:
    bool _visible;
    int _borderWidth;
    int _dropShadowWidth;
    bool _alpha;

public:
    CodeEditorOverlay(CodeEditor *editor);

    QRect rect() const;
    void paint(QPainter *painter, const QRect &clip);
    void fill(QPainter *painter, const QColor &color, const QRect &clip);

    inline bool isVisible() const { return _visible; }
    void setVisible(bool b);

    inline void hide() { setVisible(false); }
    inline void show() { setVisible(true); }

    inline void setBorderWidth(int bw) { _borderWidth = bw; }

    void update();

    void setAlpha(bool enabled) { _alpha = enabled; }

    void clear();

    enum OverlaySelectionFlags {
        LockSize = 1,
        DropShadow = 2,
        ExpandBegin = 4
    };

    void addOverlaySelection(const QTextCursor &cursor, const QColor &fg, const QColor &bg,
                             uint overlaySelectionFlags = 0, int verticalBlockSelection = 0);
    void addOverlaySelection(int begin, int end, const QColor &fg, const QColor &bg,
                             uint overlaySelectionFlags = 0, int verticalBlockSelection = 0);

    inline bool isEmpty() const { return _selections.isEmpty(); }

    inline int dropShadowWidth() const { return _dropShadowWidth; }

    bool hasCursorInSelection(const QTextCursor &cursor) const;

private:
    QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end,
                                     const QRect& clip, int verticalBlockSelection);
    void paintSelection(QPainter *painter, const OverlaySelection &selection);
    void fillSelection(QPainter *painter, const OverlaySelection &selection, const QColor &color);
};

} // namespace Internal
} // namespace CodeEdit

#endif // CODEEDITOROVERLAY_H
